let reqUrl;
let subFlag = true;
$(function () {
    //根据窗口调整表格高度
    $(window).resize(function() {
        $('#exampleTableEvents').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })

    $('#exampleTableEvents').bootstrapTable({
        url: "/stock/pageStock",
        contentType: "application/x-www-form-urlencoded",
        dataType:"json",
        method:"post",
        height:tableHeight(),//高度调整
        striped: true, //是否显示行间隔色
        pageNumber: 1, //初始化加载第一页，默认第一页
        pagination: true,//是否分页
        // dataField: "res",//获取数据的别名，先省略，则为你返回的
        sidePagination: 'server',//在服务器分页
        queryParamsType:'limit',
        queryParams:queryParams,
        pageSize: 20,//单页记录数
        clickToSelect: true,//是否启用点击选中行
        columns: [
            {
                title: 'ID',
                field: 'sid',
                align: 'center',
                visible: false
            },
            {
                title: '商品名',
                field: 'name',
                align: 'center',
                sortable: false
            },
            {
                title: '库存数量',
                field: 'sm',
                align: 'center',
                sortable: false
            },
            /*{
                title: '仓库',
                field: 'stock',
                align: 'center',
                sortable: false
            },

            {
                title: '残品',
                field: 'scr',
                align: 'center',
                sortable: false
            },*/
            {
                title: '操作',
                field: 'Button',
                align: 'center',
                events: operateEvents,//事件
                formatter: AddFunctionAlty//添加按钮
            }
        ],
        locale: 'zh-CN',//中文支持,
    });
    //点击搜索
    $('#btn-search').click(function () {
        $('#exampleTableEvents').bootstrapTable('refresh', {url: '/stock/pageStock'});//url为后台action
    })
    //点击保存

    $('#btn-submit').click(function () {
        if(!subFlag){
            return;
        }
        subFlag = false;
        let gid = $("#gid").val();
        let code = $("#code").val();
        let name = $("#name").val();
        let details = $("#details").val();
        let number = $("#number").val();
        let img = $("#img").val();
        let price = $("#price").val();
        let gcid = $("#gcid").val();
        let specification = $("#specification").val();
        let manufacturer = $("#manufacturer").val();
        var data = new FormData()
        data.append("gid",gid)
        data.append("number",number)
        data.append("code",code)
        data.append("name",name)
        data.append("details",details)
        data.append("img",img)
        data.append("price",price)
        data.append("gcid",gcid)
        data.append("manufacturer",manufacturer)
        data.append("specification",specification)

        //req(reqUrl,{gid:gid,code:code,name:name,details:details,img:img,price:price,gcid:gcid,specification:specification,manufacturer:manufacturer},number);
        req(reqUrl,data);

    })
    $("#pic").change(function () {
        //上传的请求
        let formDateObj = new FormData();
        //添加文件对象
        formDateObj.append('file',$('#pic')[0].files[0])
        //4.将对象基于ajax发送给后端
        $.ajax({
            url:'/uploadForAjax',
            type:'post',
            data:formDateObj,  //直接将对象放在data后面
            //ajax发送文件必须要指定两个参数
            contentType:false,  //不要使用任何编码，django后端能够自动识别formdata对象
            processData:false,  //告诉浏览器不要对你的数据进行任何处理
            success:function (data) {
                //回填数据到隐藏域
                $("#img").val(data.data.url);
                let img = '<img src="'+data.data.url+'" style="min-width: 80%;max-width:100%;position: absolute;padding-right: 20px" alt="">';
                $("#goodsImg").after(img);
            }
        })
    });

})

    function req(url,data) {
    $.ajax(url,{
        method:"post",
        headers:{
            contentType: "application/x-www-form-urlencoded",
        },
        contentType : false,
        processData : false,
        data:data,
        success:(res)=>{
            subFlag = true;
            if(res.status=="success"){
                swal({title: res.msg},function (){
                    window.location.reload()
                });
            }else{
                swal("操作失败！", res.msg, "error");
            }
            //清除表单数据
            document.getElementById("myform").reset();
            //隐藏模板
            $("#myModal5").modal("hide");
            //刷新表格
            $('#exampleTableEvents').bootstrapTable('refresh', {url: '/goods/pageGoods'});
        }
    })

}

    function tableHeight() {
    return $(window).height() - 150;
}

//列表行‘操作’按钮
    function AddFunctionAlty(value, row, index) {
        return '<button id="edit" type="button" class="btn btn-default">详情</button>'
    }
//请求服务数据时所传查询参数
    function queryParams(params){
        return{
            pageSize: params.limit,
            pageNum:parseInt(params.offset/params.limit)+1,
            name:$("#name_search").val(),
            code:$('#code_search').val(),
            minPrice:$('#minPrice_search').val(),
            maxPrice:$('#maxPrice_search').val(),
            minStock:$('#minStock_search').val(),
            maxStock:$('#maxStock_search').val(),
            gcid:$('#gcid_search').val()
        }
    }
//点击详情按钮事件
    window.operateEvents = {
        "click #edit": function (e, value, row, index) {
            $.ajax({
                url:"/goods/findGoodById",
                method: "post",
                data: {gid:row.gid},
                headers: {
                    contentType: "application/x-www-form-urlencoded",
                },
                success: (res) => {
                    console.log(res.data)
                    $("#title").html("库存详情");
                    $("#gid").val(res.data.gid);
                    $("#code").val(res.data.code);
                    $("#name").val(res.data.name);
                    $("#details").val(res.data.details);
                    $("#price").val(res.data.price);
                    $("#gcid").val(res.data.gcid);
                    $("#specification").val(res.data.specification);
                    $("#manufacturer").val(res.data.manufacturer);
                }
            })
            reqUrl = "/goods/updateGoods"
            $("#myModal5").modal("show");
            // window.location.href = "/getOneCadreInfo/" + row.id;//跳转新增页面
        }
            // window.location.href = "/getOneCadreInfo/" + row.id;//跳转新增页面
    }

